package com.sanshow.charging.fragment;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.sanshow.charging.MyApplication;
import com.sanshow.charging.util.GlideImageLoader;
import com.youth.banner.Banner;
import com.sanshow.charging.R;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ImageFragment extends BaseFragment {

    public Banner banner;
    public ImageView imageView;
    private Bitmap bitmap;
    private static String ROTATION_IMGPATH = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/sanshow/images/rotation/";

    public ImageFragment() {
    }

    public static ImageFragment newInstance() {
        ImageFragment fragment = new ImageFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_image, container, false);
        banner = (Banner) view.findViewById(R.id.banner);

        initBanner();
        return view;
    }

    private void initBanner() {
        // 设置轮播间隔时间
        banner.setDelayTime(5000);
        // 设置指示器的位置，小点点，左中右
        banner.setBannerStyle(BannerConfig.NOT_INDICATOR);
        //设置轮播的动画效果，内含多种特效，可点入方法内查找后内逐一体验
        banner.setBannerAnimation(Transformer.Accordion);

        // 本地图片数据（资源文件）
        List<String> list = getAllDataFileName();
        if (list != null && !list.isEmpty()) {
            banner.setImages(list)
                    .setImageLoader(new GlideImageLoader())
                    .start();
        } else {
            List<Integer> localList = new ArrayList<>();
            localList.add(R.mipmap.logo);
            banner.setImages(localList)
                    .setImageLoader(new GlideImageLoader())
                    .start();
        }

    }

    /**
     * 生成文件夹
     */
    private File makeDataFile() {
        File file = null;
        try {
            file = new File(ROTATION_IMGPATH);
            if (!file.exists()) {
                file.mkdir();
            }
        } catch (Exception e) {
            Log.i("error:", e + "");
        }
        return file;
    }

    // 获取目录下的所有文件夹
    public List<String> getAllDataFileName() {
        // 文件夹路径
        String collectionPath = makeDataFile().getPath();

        List<String> fileList = new ArrayList<>();

        File file = new File(collectionPath);
        File[] tempList = file.listFiles();

        if (tempList != null && tempList.length > 0) {
            for (int i = 0; i < tempList.length; i++) {
                if (tempList[i].isFile()) {
                    System.out.println("文     件：" + tempList[i].getName());
                    // tempList[i].toString();// 路径
                    // tempList[i].getName();// 文件名
                    // 文件名
                    String fileName = ROTATION_IMGPATH + tempList[i].getName();
                    fileList.add(fileName);
                }
            }
        }
        return fileList;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        Log.e("Image-Fragment","banner资源已释放");
        banner.releaseBanner();
    }
}